/* to do list
save file
*/


/* Build a path, append a directory slash if requested */
#include <pspkernel.h>
#include "directory.h"
#include "fileio.h"
int ReadFile(const char *location, const char *filename, unsigned char* pfilebuffer)
{
	int fileid = 0;

	char filepath[256];

	build_path(filepath, location, filename, 0);

	fileid = sceIoOpen(filepath, PSP_O_RDONLY, 0777);
	if(fileid >= 0)
	{
		int filesize;	
		filesize = sceIoLseek32(fileid, 0, PSP_SEEK_END);
		sceIoLseek(fileid, 0, PSP_SEEK_SET);
		if(filesize != sceIoRead(fileid, pfilebuffer, filesize))
			filesize = 0;	
		sceIoClose(fileid);
		return filesize;
	}
	else
	{
		//printf("Couldn't open %s!\n", filepath);
		return 0;
	}		
}

int SaveFile(const char *location, const char *filename, unsigned char* filebuffer,  int filesize)
{
	int fileid = 0;

	char filepath[256];

	build_path(filepath, location, filename, 0);
	
	fileid = sceIoOpen(filepath, PSP_O_CREAT | PSP_O_TRUNC | PSP_O_WRONLY, 0777);
	
	if(fileid < 0)
		return -1;
	sceIoWrite(fileid, filebuffer, filesize);
	sceIoClose(fileid);
	return 0;
}

int GetFileSize(const char *location, const char *filename)
{
	int fileid = 0;

	char filepath[256];

	build_path(filepath, location, filename, 0);

	fileid = sceIoOpen(filepath, PSP_O_RDONLY, 0777);
	if(fileid >= 0)
	{
		int filesize;	
		filesize = sceIoLseek32(fileid, 0, PSP_SEEK_END);
		sceIoClose(fileid);
		return filesize;
	}
	else
	{
		//printf("Couldn't open %s!\n", filepath);
		return 0;
	}
}

